Learning Flex Basics (Part 3) : Working with Containers
Table of Contents
- Introduction
- Building the Containers
- Create the Application Object
- Create a Data Array
- Add a TabNavigator Container for Global Navigation
- Add an HBox and Panel Containers for Main Views
- Add a ViewStack, LinkBar, and Panel Containers for Secondary Views
- Add a Form Container
- Add a Tile Layout Container and Populate It Using a Repeater
- Create a Pseudo Bar Chart Using a Repeater and VBoxes
- Complete Code Listing for containers.mxml
- Where to Go from Here
Create a Pseudo Bar Chart Using a Repeater and VBox Containers
In the final part of this tutorial, you deal with no new Flex classes, but instead put the VBox container to a different use: simulating a bar chart, to get a sense of the flexibility of containers.
You also learn how to change properties of repeated items at runtime. The key to this is
knowing how to access the repeated items by id. All id values in an application must be
unique, so if you set the id of a repeated item to a value like "myBox," myBox becomes an
array whose elements are the id values of the items. You can then access the properties
of the ith item through myBox[i].
To loop over all the repeated items, loop over the id array:
for (var i:Number=0;i<myBox.length;i++)
{
[do something to mybox[i]...]
}
-
Inside the Panel container with the id of "barPanel," add an HBox container with height bound to 80% of the surrounding Panel's height, and contents vertically aligned to the bottom:
<mx:HBox height="{.8*barPanel.height}" verticalAlign="bottom"> </mx:HBox> -
Inside the HBox container, add a Repeater object with the id "barRep" and the dataProvider bound to myArray:
<mx:Repeater id="barRep" dataProvider="{myArray}"> </mx:Repeater> -
Inside the Repeater object, add a VBox container with an
idof vBar abackgroundColorof 0x99CCCC, height set to 20 times thecurrentItemof the Repeater, and contents horizontally aligned to the center:<mx:VBox id="vBar" backgroundColor="0x99CCCC" height="{20*barRep.currentItem}" width="40" horizontalAlign="center"> </mx:VBox> -
Inside the VBox container, add a Label whose text property is bound the currentItem of the Repeater object:
<mx:Label text="{barRep.currentItem}"/> -
After the HBox container, but still inside the Panel container, add a ControlBar container:
<mx:ControlBar> </mx:ControlBar>
-
Inside the ControlBar, add two Buttons, one with the label Scale Up and a click event that calls a function called
scaleUp(), the other with the label Scale Down and a click event that callsscaleDown():<mx:Button label="Scale Up" click="scaleUp()"/> <mx:Button label="Scale Down" click="scaleDown()"/>
-
In the Script block, add the
scaleUp()function block, the function takes no parameters and returns nothing:function scaleUp():Void { } -
Inside the function block, create a for loop that loops over the
vBararray :for (var i:Number=0;i<vBar.length;i++) { } -
Inside the for loop, set the height of each
vBaritem to twice its current height:vBar[i].height=vBar[i].height*2;
-
Inside the function block, create a for loop that loops over the
vBararray :for (var i:Number=0;i<vBar.length;i++) { vBar[i].height=vBar[i].height*2; } -
Add the
scaleDown()function, which is identical toscaleUp()except that it sets eachvBarheight to half its current value:function scaleDown():Void { for (var i:Number=0;i<vBar.length;i++) { vBar[i].height=vBar[i].height/2; } } -
That's the end of the exercise. Save the file and browse it; try the buttons to see the bars in the graph scaled up and down.